package com.eastfair.venueservice.service.impl;

import cn.hutool.core.bean.BeanUtil;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.boot.request.PageParams;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.venueservice.dao.HomeBuilderDrawingCategoryAuditHistoryMapper;
import com.eastfair.venueservice.dto.HomeBuilderDrawingCategoryAuditHistoryDTO;
import com.eastfair.venueservice.entity.HomeBuilderDrawingCategoryAuditHistory;
import com.eastfair.venueservice.service.HomeBuilderDrawingCategoryAuditHistoryService;
import com.eastfair.venueservice.service.VenueManageReportDrawingCategoryAuditHistoryService;
import com.eastfair.venueservice.vo.HomeBuilderDrawingCategoryAuditHistoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 主场搭建商图纸分类审核历史
 * </p>
 *
 * @author dqq
 * @date 2023-05-04
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class HomeBuilderDrawingCategoryAuditHistoryServiceImpl extends SuperServiceImpl<HomeBuilderDrawingCategoryAuditHistoryMapper, HomeBuilderDrawingCategoryAuditHistory> implements HomeBuilderDrawingCategoryAuditHistoryService {

    @Resource
    private VenueManageReportDrawingCategoryAuditHistoryService venueManageReportDrawingCategoryAuditHistoryService;

    @Resource(name = "threadPoolTaskExecutor")
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<HomeBuilderDrawingCategoryAuditHistory> modelList) {
        modelList.forEach(s -> s.setIsDeleted(BusinessConstant.YES));
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(HomeBuilderDrawingCategoryAuditHistory model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    @Override
    public Page<HomeBuilderDrawingCategoryAuditHistoryVO> listHistoryVO(PageParams<HomeBuilderDrawingCategoryAuditHistoryDTO> pageParams) {
        log.info("listHistoryVO - 分页查询审核历史, pageParams={}", pageParams);
        QueryWrapper<HomeBuilderDrawingCategoryAuditHistory> queryWrapper = createQueryWrapper(pageParams.getModel());
        Page<HomeBuilderDrawingCategoryAuditHistory> page = page(pageParams.buildPage(), queryWrapper);
        Page<HomeBuilderDrawingCategoryAuditHistoryVO> voPage = new Page<>();
        BeanUtil.copyProperties(page, voPage);
        List<HomeBuilderDrawingCategoryAuditHistory> list = page.getRecords();
        if (list == null || list.isEmpty()) {
            return voPage;
        }
        List<HomeBuilderDrawingCategoryAuditHistoryVO> voList = list
                .stream()
                .map(homeBuilderDrawingCategoryAuditHistory -> BeanUtil.toBean(homeBuilderDrawingCategoryAuditHistory, HomeBuilderDrawingCategoryAuditHistoryVO.class))
                .collect(Collectors.toList());
        voPage.setRecords(voList);
        return voPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveHistory(HomeBuilderDrawingCategoryAuditHistoryDTO historyDTO) {
        log.info("saveHistory - 保存审核历史, historyDTO={}", historyDTO);
        HomeBuilderDrawingCategoryAuditHistory history = BeanUtil.toBean(historyDTO, HomeBuilderDrawingCategoryAuditHistory.class);
        save(history);
        // 更新主场图纸分类审核历史
        venueManageReportDrawingCategoryAuditHistoryService.updateVenueAuditStatus(history);
        return true;
    }

    @Override
    public void asyncSaveHistory(HomeBuilderDrawingCategoryAuditHistoryDTO historyDTO) {
        threadPoolTaskExecutor.execute(() -> saveHistory(historyDTO));
    }

    private QueryWrapper<HomeBuilderDrawingCategoryAuditHistory> createQueryWrapper(HomeBuilderDrawingCategoryAuditHistoryDTO query) {
        QueryWrapper<HomeBuilderDrawingCategoryAuditHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda()
                .eq(HomeBuilderDrawingCategoryAuditHistory::getIsDeleted, BusinessConstant.DELETE_NO)
                .eq(query.getReportId() != null, HomeBuilderDrawingCategoryAuditHistory::getReportId, query.getReportId())
                .eq(query.getDrawingId() != null, HomeBuilderDrawingCategoryAuditHistory::getDrawingId, query.getDrawingId())
                .eq(query.getExhibitionManageId() != null, HomeBuilderDrawingCategoryAuditHistory::getExhibitionManageId, query.getExhibitionManageId())
        ;
        return queryWrapper;
    }
}
